package  com.baomidou.springwind.controller;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.framework.common.util.DateUtil;
import com.baomidou.framework.controller.SuperController;
import com.baomidou.framework.upload.UploadFile;
import com.baomidou.framework.upload.UploadMsg;
import com.baomidou.framework.upload.UploadMultipartRequest;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.springwind.common.HoorayosHelper;
import com.baomidou.springwind.entity.App;
import com.baomidou.springwind.entity.AppCategory;
import com.baomidou.springwind.entity.Member;
import com.baomidou.springwind.entity.MemberApp;
import com.baomidou.springwind.entity.MyPermission;
import com.baomidou.springwind.entity.Permission1;
import com.baomidou.springwind.service.IAppCategoryService;
import com.baomidou.springwind.service.IAppService;
import com.baomidou.springwind.service.IMemberService;
import com.baomidou.springwind.service.IPermission1Service;
import com.baomidou.springwind.service.IPermissionService;

/**
 *  应用管理控制器首页
 * @author koumiba
 * @Date 2016-04-13
 */
@Controller
@RequestMapping("/sysapp/permission")
public class Permission1Controller extends SuperController {
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IPermission1Service permissonService;
	@Autowired
	private IAppService appService;
	@Autowired
	private IAppCategoryService appCategoryService;
	/**
	 * 主页
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("errorCode", HoorayosHelper.getErrorCodeMap());
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noLogin"));
			return "error";
		}
		//验证是否管理员
		int mid = HoorayosHelper.getMemberId(request);
		Member user = memberService.selectById(mid);
		if(!HoorayosHelper.checkAdmin(user)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noAdmin"));
			return "error";
		}
		// 验证权限
		int userPermissionId = user.getPermissionId().intValue(); 
		String appsId = permissonService.selectById(userPermissionId).getAppsId();
		if(!HoorayosHelper.checheckPermissions(appsId, "5")){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noPermissions"));
			return "error";
		}
		
		return "/sysapp/permission/index";
	}
	/**
	 * 详情
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/detail")
	public String detail(Model model) {
		model.addAttribute("errorCode", HoorayosHelper.getErrorCodeMap());
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noLogin"));
			return "error";
		}
		//验证是否管理员
		int mid = HoorayosHelper.getMemberId(request);
		Member user = memberService.selectById(mid);
		if(!HoorayosHelper.checkAdmin(user)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noAdmin"));
			return "error";
		}
		// 验证权限
		int userPermissionId = user.getPermissionId().intValue(); 
		String appsId = permissonService.selectById(userPermissionId).getAppsId();
		if(!HoorayosHelper.checheckPermissions(appsId, "5")){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noPermissions"));
			return "error";
		}
		
		String permissionId = request.getParameter("permissionid");
		if(permissionId!=null && !permissionId.equals("")){
			Permission1 permission = permissonService.selectById(permissionId);
			if(permission!=null && !permission.getAppsId().equals("")){
				EntityWrapper ew = new EntityWrapper<App>();
				ew.in("tbid", permission.getAppsId());
				List<App> appsInfo = new App().selectList(ew);
				model.addAttribute("appsinfo", appsInfo);
				model.addAttribute("permission", permission);
			}			
		}
		model.addAttribute("permissionid", permissionId);
		return "/sysapp/permission/detail";
	}
	/**
	 * 添加应用
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/alert_addapps")
	public void addApps(Model model) {
		EntityWrapper ew = new EntityWrapper<AppCategory>();
		ew.where("issystem=1");
		AppCategory ac = new AppCategory().selectOne(ew);
		
		ew = new EntityWrapper<App>();
		ew.where("app_category_id="+ac.getTbid());
		ew.setSqlSelect("tbid, name, icon");
		List<App> appList = new App().selectList(ew);
		
		model.addAttribute("applist", appList);
	}
	/**
	 * ajax页面
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/ajax")
	public String ajax(String ac) {
		String display = "";
		if (ac.equals("getList")){
			String search_1 = request.getParameter("search_1");
			int from = Integer.parseInt(request.getParameter("from"));
			int to = Integer.parseInt(request.getParameter("to"));
			EntityWrapper ew = new EntityWrapper<Permission1>();
			
			if(search_1!=null && !search_1.equals("")){
				ew.like("name", search_1);
			}
			List<Permission1> permissionList = new Permission1().selectList(ew);
			display +=permissionList.size() + "<{|*|}>";
			for(int i=from; i<=to && i<permissionList.size(); i++){
				Permission1 per = permissionList.get(i);
				display += "<tr class='list-bd'>";
				display += "<td style='text-align:left;padding-left:15px'>"+per.getName()+"</td>";
				display += "<td><a href='javascript:openDetailIframe(\"detail.html?permissionid="+per.getTbid()+"\");' class='btn btn-link'>编辑</a><a href='javascript:;' class='btn btn-link do-del' permissionid='"+per.getTbid()+"'>删除</a></td>";
				display += "</tr>";
			}
		}else if (ac.equals("del")){
			int tbid = Integer.parseInt(request.getParameter("permissionid"));
			permissonService.deleteById(tbid);
		}
		return display;
	
	}
	
	/**
	 * ajax页面
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/detailAjax")
	public String detailAjax(String ac) {
		
		if (ac.equals("edit")){
			String id = request.getParameter("id");
			String valName = request.getParameter("val_name");
			String valAppsId = request.getParameter("val_apps_id");
			
			Permission1 data = new Permission1();
			if(id!=null && !id.equals("")){
				data = permissonService.selectById(Integer.parseInt(id));
			}
			data.setName(valName);
			data.setAppsId(valAppsId);
			
			if(id==null || id.equals("")){
				EntityWrapper ew = new EntityWrapper<Permission1>();
				ew.orderBy("tbid", false);
				Permission1 per = new Permission1().selectOne(ew);
				int nextId = per.getTbid().intValue()+1;
				data.setTbid((long)nextId);
				permissonService.insert(data);
			}else{
				permissonService.insertOrUpdate(data);
			}		
			Map<String,String> result = new HashMap<String,String>();
			result.put("info", "");
			result.put("status", "y");
			return JSONUtils.toJSONString(result);
		}else if(ac.equals("updateApps")){
			String appsId = request.getParameter("appsid");
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", appsId);
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			String result = "";
			for(App a : appList){
				result += "<div class='app' appid='"+a.getTbid()+"'><img src='"+a.getIcon()+"' alt='"+a.getName()+"' title='"+a.getName()+"'><span class='del'>删</span></div>";
			}
			return result;
		}
		return "";
	}
	
	
	
	/*************************
	 *    私有方法 
	 *************************/
	
	
}
